package com.newpage.module_lecture.adapter;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.newpage.module_lecture.R;
import com.wetao.lib_common.bean.response.ExplainBean;

import java.util.List;


/**
 * 来源适配器
 */
public class ExplainTeacherAdapter extends RecyclerView.Adapter{


    private List<ExplainBean> mData;
    private ExplainBean selectExplain;
    public ExplainTeacherAdapter(List<ExplainBean> mData,
                                 int pos,ExplainBean explainBean) {
        this.mData = mData;
        selectExplain = explainBean;
    }

    public void updateData(List<ExplainBean> data){
        mData = data;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == 1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lecture_teacher_item_pop, parent, false);
            return new ExplainTeacherViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ExplainTeacherViewHolder) {
            ((ExplainTeacherViewHolder)holder).bindData(position);
        }
    }

    @Override
    public int getItemCount() {
        if(mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }



    class ExplainTeacherViewHolder<T> extends RecyclerView.ViewHolder{
        TextView tv_pop;

        public ExplainTeacherViewHolder( View itemView) {
            super(itemView);
           tv_pop = itemView.findViewById(R.id.tv_teacher_pop);
        }
        public void bindData(final int position) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(position);
                        tv_pop.setText(mData.get(position).getExplainName());
                        tv_pop.setSelected(true);
                    }
                }
            });

            if (selectExplain.getExplainName().equals(mData.get(position).getExplainName())
                    && selectExplain.getId().equals(mData.get(position).getId())){
                tv_pop.setSelected(true);
            }else {
                tv_pop.setSelected(false);
            }
            tv_pop.setText("" + mData.get(position).getExplainName());

        }
    }



    //第一步 定义接口
    public interface OnItemClickListener {
        void onClick(int position);
    }
    private OnItemClickListener listener;
    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
